package de.szut.ita13sommerfeld.apacheanalyser.apachelog;

import de.szut.ita13sommerfeld.apacheanalyser.libaries.EntryLibary;
import de.szut.ita13sommerfeld.apacheanalyser.libaries.ResourceLibary;

/**
 * 
 * @author Rene Sommerfeld
 *
 */
public class User {
	
	private String ip;
	private EntryLibary entries;
	private ResourceLibary resources;
	private Date firstAction;
	private Date lastAction;
	private long totalsize;
	
	/**
	 * 
	 * @param ip is one ip of the log thats assigned to the user
	 * @param firstAction is the date of the first action of the user
	 */
	public User(String ip, Date firstAction, Date lastAction) {
		this.ip = ip;
		this.firstAction = firstAction;
		this.lastAction = lastAction;
		entries = new EntryLibary();
		resources = new ResourceLibary();
		totalsize = 0;
	}

	/**
	 * 
	 * @return returns the ip of the user
	 */
	public String getIp() {
		return ip;
	}

	/**
	 * 
	 * @param ip sets this ip
	 */
	public void setIp(String ip) {
		this.ip = ip;
	}

	/**
	 * 
	 * @return returns the EntryLibary of the user
	 */
	public EntryLibary getEntries() {
		return entries;
	}

	/**
	 * 
	 * @param entries sets this EntryLibary
	 */
	public void setEntries(EntryLibary entries) {
		this.entries = entries;
	}

	/**
	 * 
	 * @return returns the date of the first action
	 */
	public Date getFirstAction() {
		return firstAction;
	}

	/**
	 * 
	 * @param firstAction sets this first action
	 */
	public void setFirstAction(Date firstAction) {
		this.firstAction = firstAction;
	}

	/**
	 * 
	 * @return returns the date of the last action
	 */
	public Date getLastAction() {
		return lastAction;
	}

	/**
	 * 
	 * @param lastAction sets this last action
	 */
	public void setLastAction(Date lastAction) {
		this.lastAction = lastAction;
	}

	/**
	 * 
	 * @return returns the total data size of an user
	 */
	public long getTotalsize() {
		return totalsize;
	}

	/**
	 * 
	 * @param totalsize
	 */
	public void setTotalsize(long totalsize) {
		this.totalsize = totalsize;
	}
	
	/**
	 * 
	 * @param size add size of data to total data size
	 */
	public void addDataSize(int size) {
		totalsize += size;
	}

	/**
	 * 
	 * @return returns the ResourceLibary of an user
	 */
	public ResourceLibary getResources() {
		return resources;
	}

	/**
	 * 
	 * @param resources sets this ResourceLibary
	 */
	public void setResources(ResourceLibary resources) {
		this.resources = resources;
	}
	
	/**
	 * 
	 * @param user the current user that will get the resource
	 * @param resource the resource that will assigned to the user
	 */
	public static void addResource(User user, String resource) {
		if(!user.getResources().containsKey(resource)) {
			user.getResources().put(resource, new Integer(1));
		} else {
			user.getResources().put(resource, user.getResources().get(resource) + 1);
		}
	}
}
